package com.demo.example;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@Slf4j
@RestController
@SpringBootApplication
public class Application implements ApplicationRunner {
    private String jarInformationVersion;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @GetMapping("/version")
    public String getVersion() {
        return jarInformationVersion;
    }

    @Override
    public void run(ApplicationArguments args) {
        String jarFilePath = getJarFilePath();
        jarInformationVersion = ManifestUtils.getInformationVersion(jarFilePath);
        log.info("\n----------------------------------------\n\t" + "应用启动成功" + "\n\t" +
                "jar路径:[{}]" + "\n\t" +
                "jar版本号:[{}]\n----------------------------------------", jarFilePath, jarInformationVersion);
    }


    private String getJarFilePath() {
        //linux和windows下通用
        ApplicationHome home = new ApplicationHome(getClass());
        File jarFile = home.getSource();
        return jarFile.getParentFile().toString() + File.separator + "springboot-build-svn-number.jar";
    }

}
